﻿using PluginAPI.Core;
using System.Collections.Generic;


namespace ReconnectRole.Data
{
    /// <summary>
    /// 重连清单
    /// </summary>
    public class ReconnectList
    {
        /// <summary>
        /// 断线清单
        /// | 数据结构：<SteamID, 角色信息>
        /// </summary>
        private Dictionary<string, RoleInfo> m_DisConnectList;


        public ReconnectList()
        {
            m_DisConnectList = new Dictionary<string, RoleInfo>();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        public void DoRecord(RoleInfo info)
        {
            bool isContain = this.m_DisConnectList.ContainsKey(info.SteamID);
            if (isContain)
            {
                PluginAPI.Core.Log.Warning($"" +
                    $"|异常日志" +
                    $"|{info.SteamID}|用户信息已存在|覆盖更新" +
                    $"|写入身份：{info.RoleTypeId}" +
                    $"|缓存身份：{m_DisConnectList[info.SteamID].RoleTypeId}");

                this.m_DisConnectList[info.SteamID] = info;

                return;
            }

            PluginAPI.Core.Log.Info($"" +
                $"|日志" +
                $"|{info.SteamID}|用户信息已写入" +
                $"|写入身份：{info.RoleTypeId}" +
                $"|写入时间：{info.LeftTime:HH:mm:ss}");

            this.m_DisConnectList.Add(info.SteamID, info);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="steamID">Steam用户ID</param>
        public void DoRemove(string steamID)
        { 
            bool isContain = this.m_DisConnectList.ContainsKey(steamID);
            if (isContain)
            {
                this.m_DisConnectList.Remove(steamID);

                PluginAPI.Core.Log.Info($"" +
                    $"|记录日志" +
                    $"|{steamID}|用户信息已移除");

                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="steamID"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public bool TryRead(string steamID, out RoleInfo output)
        { 
            bool isContain = this.m_DisConnectList.ContainsKey(steamID);

            if (isContain)
            {
                output = this.m_DisConnectList[steamID];
                return true;
            }

            output = null;

            return false;
        }


        /// <summary>
        /// 
        /// </summary>
        public void DoUpdate()
        {
            if (m_DisConnectList == null) return;
            if (m_DisConnectList.Count == 0) return;


            foreach (var item in m_DisConnectList)
            {
                Player match = Player.GetPlayers().Find(x => x.UserId == item.Value.SteamID);

                if (match == null) continue;
                if (match.Role != item.Value.RoleTypeId) continue;

                item.Value.Position = match.Position;
                item.Value.LeftTime = System.DateTime.Now;
                item.Value.Health = match.Health;
            }
        }


        public void DoClear()
        {
            if (this.m_DisConnectList == null) return;

            PluginAPI.Core.Log.Info($"" +
                $"|记录日志" +
                $"|缓存已清空");

            this.m_DisConnectList.Clear();
        }
    }
}
